package pew.pong;

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.network.Client;
import com.jme3.network.Network;
import com.jme3.network.serializing.Serializer;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import pew.pong.network.message.ShutdownServerMessage;

/**
 * test
 * @author normenhansen
 */
public class PongClient extends SimpleApplication {

    public static void main(String[] args) {
	String host = "localhost";
	int port = 6143;
	
	if (args.length > 0) {
	    host = args[0];
	}
	if (args.length > 1) {
	    port = Integer.parseInt(args[1]);
	}
	
        PongClient app = new PongClient(host, port);
        app.start();
    }
    private Client client;
    private final int port;
    private final String host;

    private PongClient(String host, int port) {
	this.host = host;
	this.port = port;
    }

    @Override
    public void simpleInitApp() {
        try {
	    Serializer.registerClass(ShutdownServerMessage.class);
            Box b = new Box(Vector3f.ZERO, 1, 1, 1);
            Geometry geom = new Geometry("Box", b);

            Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
            mat.setColor("Color", ColorRGBA.Blue);
            geom.setMaterial(mat);

            rootNode.attachChild(geom);
            
            client = Network.connectToServer(host, port);
            client.start();
        } catch (IOException ex) {
            Logger.getLogger(PongClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }

    @Override
    public void destroy() {
      client.close();
        super.destroy();
    }
}
